package com.ruoyi.web.controller.hk;

import com.ruoyi.hk.domain.HSysUser;
import com.ruoyi.hk.domain.User;
import com.ruoyi.hk.domain.dto.UserDto;
import com.ruoyi.hk.service.UserService;
import com.ruoyi.hk.hanler.enums.BusinessCode;
import com.ruoyi.hk.hanler.exception.HBusinessException;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/addUser")
    @ApiOperation("用户注册")
    public ResponseEntity<?> addUser(@RequestBody UserDto userDto){
        try {
            Integer rows = userService.addUser(userDto);
            if (rows > 0) {
                return ResponseEntity.ok(AjaxResult.success("注册成功"));
            } else {
                return ResponseEntity.ok(AjaxResult.error("注册失败"));
            }
        } catch (Exception e) {
            // 异常会被全局异常处理器捕获，这里不需要额外处理
            throw e;
        }
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(HBusinessException.class)
    public ResponseEntity<AjaxResult> handleHBusinessException(HBusinessException e) {
        if (e.getResultCode() != null) {
            return ResponseEntity.ok(AjaxResult.error(e.getResultCode().getCode(), e.getResultCode().getMsg()));
        }
        return ResponseEntity.ok(AjaxResult.error(e.getMessage()));
    }

    @PostMapping("updateDoctorQX")
    @ApiOperation("医生账号确认")
    public ResponseEntity<?> updateDoctorQX(HSysUser user){
        Integer rows = userService.updateDoctorQX(user);
        if (rows > 0){
            return ResponseEntity.ok(AjaxResult.success("账户确认成功"));
        }else {
            return ResponseEntity.ok(AjaxResult.error("账户确认失败"));
        }

    }
}
